package com.zzyl.nursing.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.nursing.domain.NursingProject;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import com.zzyl.nursing.vo.NursingProjectVo;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.reactive.TransactionalOperator;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 护理计划Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-31
 */
@Transactional
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Integer id) {
        //1.根据id查询护理计划
        NursingPlan nursingPlan = this.getById(id);
        NursingPlanVo vo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan, vo);
        //2.查询关联的护理项目计划关系
        LambdaQueryWrapper<NursingProjectPlan> qw = new LambdaQueryWrapper<>();
        qw.eq(NursingProjectPlan::getPlanId, id);
        //3.查询关联的护理项目
        List<NursingProjectPlan> nursingProjectPlans = nursingProjectPlanMapper.selectNursingProjectPlanById(id);
        List<NursingProjectPlanVo> collect = nursingProjectPlans.stream().map(nursingProjectPlan -> {
            NursingProjectPlanVo nursingProjectPlanVo = new NursingProjectPlanVo();
            BeanUtils.copyProperties(nursingProjectPlan, nursingProjectPlanVo);
            nursingProjectPlanVo.setProjectId(String.valueOf(nursingProjectPlan.getProjectId()));
            return nursingProjectPlanVo;
        }).collect(Collectors.toList());
        vo.setProjectPlans(collect);
        return vo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        QueryWrapper<NursingPlan> qw = new QueryWrapper<>();
        return nursingPlanMapper.selectList(qw);
    }

    /**
     * 新增护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Transactional
    @Override
    public int insertNursingPlan(NursingPlanDto dto) {
        // 新增护理计划
        // 属性拷贝
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto, nursingPlan);
        nursingPlanMapper.insertNursingPlan(nursingPlan);
        // 批量保存护理项目计划关系
        int count = nursingProjectPlanMapper.batchInsert(dto.getProjectPlans(), nursingPlan.getId());
        return count;
    }

    /**
     * 修改护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int updateNursingPlan(NursingPlanDto dto) {
        try {
            //1.属性拷贝
            NursingPlan nursingPlan = new NursingPlan();
            BeanUtils.copyProperties(dto, nursingPlan);
            int num = nursingPlanMapper.updateNursingPlan(nursingPlan);
            //2.判断dto中有没有关联的护理项目，如果不为空，则先删除护理项目与护理计划的关系，再重新批量添加
            List<NursingProjectPlan> projectPlans = dto.getProjectPlans();
            if (!CollectionUtils.isEmpty(projectPlans)) {
                // 删除护理计划对应的护理项目列表
                nursingProjectPlanMapper.delete(
                        new LambdaQueryWrapper<NursingProjectPlan>()
                                .eq(NursingProjectPlan::getPlanId, nursingPlan.getId()));
                for (NursingProjectPlan projectPlan : projectPlans) {
                    projectPlan.setPlanId(nursingPlan.getId());
                    nursingProjectPlanMapper.insert(projectPlan);
                }
            }
            //不管项目是否为空，都要修改护理计划
            return num;
        } catch (Exception e) {
            log.error("修改护理计划异常", e);
            throw new RuntimeException("修改护理计划失败", e);
        }
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Integer id) {
        //1.删除护理计划与护理项目的关系
        nursingProjectPlanMapper.deleteByPlanId(id);
        //2.删除护理计划
        return removeById(id) ? 1 : 0;
    }

    @Override
    public List<NursingPlan> selectAll() {
        return nursingPlanMapper.selectAll();
    }
}
